package com.zmzncs.lmtc.module.park.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zmzncs.lmtc.common.pojo.PageVO;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.park.pojo.parkinfo.*;
import com.zmzncs.lmtc.module.park.service.ParkInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-09
 */
@Api(tags = "停车场信息")
@RestController
@RequestMapping("/park/info")
public class ParkInfoController {

    @Autowired
    ParkInfoService parkInfoService;


    @ApiOperation(value = "获取最近预约停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType = "query", name = "longitude", value = "经度", required = true),
            @ApiImplicitParam(paramType = "query", name = "latitude", value = "维度", required = true),
    })
    @GetMapping(value = "/getNearbyReserveParkList")
    public Rest<NearParkInfoVO> getNearbyReserveParkList(@NotBlank String longitude, @NotBlank String latitude){
        ParkInfoDTO parkInfoDTO = parkInfoService.getNearbyReserveParkList(longitude,latitude);
        return RestUtil.success(parkInfoDTO);
    }

    @ApiOperation(value = "获取预约停车场列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "parkName", value = "名称", required = true),
    })
    @GetMapping(value = "/getReserveParkList")
    public Rest<List<ParkInfoVO>> getReserveParkList(@NotBlank String parkName){
        List<ParkInfoDTO> parkInfoDTOList = parkInfoService.getReserveParkList(parkName);
        List<ParkInfoVO> parkInfoVOList = JsonUtil.listTransform(parkInfoDTOList, ParkInfoVO.class);
        return RestUtil.success(parkInfoVOList);
    }


    @ApiOperation(value = "获取停车场列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping(value = "/getParkList")
    public Rest<List<ParkInfoVO>> getParkList(){
        List<ParkInfoDTO> parkInfoDTOList = parkInfoService.getParkList();
        List<ParkInfoVO> parkInfoVOList = JsonUtil.listTransform(parkInfoDTOList, ParkInfoVO.class);

        return RestUtil.success(parkInfoVOList);
    }

    @ApiOperation(value = "查询附近停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "currentPage", value = "页码", required = true),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true),
            @ApiImplicitParam(paramType = "query", name = "longitude", value = "经度", required = true),
            @ApiImplicitParam(paramType = "query", name = "latitude", value = "维度", required = true),
    })
    @GetMapping(value = "getNearbyParkList")
    public Rest<PageVO<ParkInfoNearVO>> getNearbyParkList(@NotNull Integer currentPage, @NotNull Integer pageSize, @NotBlank String longitude, @NotBlank String latitude) {
        IPage<ParkInfoDTO> nearbyParkIPage = parkInfoService.getNearbyParkList(new Page(currentPage, pageSize), longitude, latitude);
        PageVO<ParkInfoNearVO> parkInfoVOPageVO = JsonUtil.pageTransform(nearbyParkIPage, ParkInfoNearVO.class);

        return RestUtil.success(parkInfoVOPageVO);
    }

    @ApiOperation(value = "查询附近有月租规则的停车场")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "currentPage", value = "页码", required = true),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true),
            @ApiImplicitParam(paramType = "query", name = "longitude", value = "经度", required = true),
            @ApiImplicitParam(paramType = "query", name = "latitude", value = "维度", required = true),
    })
    @GetMapping(value = "get-nearby-park-has-month-rule-list")
    public Rest<PageVO<ParkInfoNearMonthRuleVO>> getNearbyParkHasMonthRuleList(@NotNull Integer currentPage, @NotNull Integer pageSize, @NotBlank String longitude, @NotBlank String latitude) {
        IPage<ParkInfoDTO> nearbyParkIPage = parkInfoService.getNearbyParkHasMonthRuleList(new Page(currentPage, pageSize), longitude, latitude);
        PageVO<ParkInfoNearMonthRuleVO> parkInfoVOPageVO = JsonUtil.pageTransform(nearbyParkIPage, ParkInfoNearMonthRuleVO.class);

        return RestUtil.success(parkInfoVOPageVO);
    }

    @ApiOperation(value = "模糊查询停车场（名称、地址）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "keyword", value = "名称、地址", required = true),
    })
    @GetMapping(value = "getParkByName")
    public Rest<List<ParkInfoNameVO>> getParkByName(@NotNull String keyword) {
        List<ParkInfoDTO> parks = parkInfoService.getParkByName(keyword);
        List<ParkInfoNameVO> parkInfoVOList = JsonUtil.listTransform(parks, ParkInfoNameVO.class);

        return RestUtil.success(parkInfoVOList);
    }


    @ApiOperation(value = "查询停车场剩余可预约车位数")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "parkId", value = "停车场Id", required = true),
            @ApiImplicitParam(paramType="query", name = "reserveDate", value = "预约日期", required = true),
    })
    @GetMapping(value = "getParkReservesSpace")
    public Rest<Integer> getParkReservesSpace(@NotNull Long parkId, @DateTimeFormat(pattern="yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd")Date reserveDate) {
        Integer count = parkInfoService.getParkReservesSpace(parkId,reserveDate);

        return RestUtil.success(count);
    }

    @ApiOperation(value = "ID查询停车场信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name = "parkId", value = "停车场Id", required = true),
    })
    @GetMapping(value = "getParkInfoById")
    public Rest<ParkInfoOneVO> getParkInfoById(@NotNull Long parkId) {
        ParkInfoDTO parkInfoDTO = parkInfoService.getParkInfoById(parkId);
        ParkInfoOneVO parkInfoOneVO = new ParkInfoOneVO();
        BeanUtils.copyProperties(parkInfoDTO,parkInfoOneVO);
        return RestUtil.success(parkInfoOneVO);
    }


}

